package org.cgc.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.cgc.obj.Graph;

public class CreateWindow extends JDialog {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//GUI
	private JLabel lInfo, lSize;
	private JTextField tfSize;
	private JButton bOk;
	
	private GCWindow parent;
	
	public CreateWindow(GCWindow parent) {
		this.parent = parent;
		
		setTitle("Create new graph");
		initView();
		pack();
		setVisible(true);
	}
	
	private void initView() {
		String info =
			"<html>" +
			"Create new empty graph with specified size." +
			"<li>Size - number of vertices" +
			"</html>";
		
		lInfo = new JLabel(info);
		lSize = new JLabel("Size: ");
		tfSize = new JTextField();
		bOk = new JButton("OK");
		bOk.addActionListener(new OKListener());
		JPanel pane = new JPanel(new GridLayout(6, 1));
		pane.add(lInfo);
		pane.add(lSize);
		pane.add(tfSize);
		pane.add(bOk);
		
		add(pane);
	}
	
	private class OKListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			int size;
			try {
				size = Integer.parseInt(tfSize.getText());
			} catch(NumberFormatException ex) {
				JOptionPane.showMessageDialog(CreateWindow.this,
					    "Cannot initialize variable.",
					    "Number format error",
					    JOptionPane.ERROR_MESSAGE);
				return;
			}
			new Graph(size, 0).copyTo(parent.getGraph());
			parent.repaint();
			CreateWindow.this.dispose();
		}
		
	}
}
